package com.vegas.netspider.apps;

import java.util.Calendar;

public class UrlUtil {
	
	private static String domainTag = "\\$\\[DOMAIN\\]";
	private static String sessionTag = "\\$\\[SESSION\\]";
	private static String pageTag = "\\$\\[PAGE\\]";
	private static String dateTag = "\\$\\[DATE\\]";
	private static String langxTag = "\\$\\[LANGX\\]";
	private static String matchIdTag = "\\$\\[MATCH_ID\\]";
	
	
	public static final String replaceMatchId(String url,Integer matchId){
		if(matchId==null){
			return url;
		}
		
		return url.replaceFirst(matchIdTag, matchId.intValue()+"");
	}

	public static final String replaceDomain(String url,String domain){
		if(domain!=null){
			return url.replaceFirst(domainTag, domain);			
		}
		
		return url;
	}
	
	public static final String replaceSessionId(String url,String sessionId){
		if(sessionId!=null){
			return url.replaceFirst(sessionTag, sessionId);
		}
		
		return url;
	}
	
	public static final String replacePage(String url,int page){
		return url.replaceFirst(pageTag, page+"");			
	}
	
	public static final String replaceDate(String url,Calendar date){
		if(date==null){
			return url;
		}
		
		String d = date.get(Calendar.YEAR)+"-"+(date.get(Calendar.MONTH)+1)+"-"+date.get(Calendar.DAY_OF_MONTH);
		
		return url.replaceFirst(dateTag, d);
	}
	
	public static final String replaceLangx(String url,String langx){
		if(langx==null){
			return url;
		}
		
		return url.replaceFirst(langxTag, langx);
	}
	
	public static final String replace(String url,String domain,String sessionid,int page,Calendar date,String langx){
		String _url = null;
		
		_url = replaceDomain(url,domain);
		
		_url = replaceSessionId(_url,sessionid);
		
		_url = replacePage(_url,page);
		
		_url = replaceDate(_url, date);
		
		_url = replaceLangx(_url, langx);
		
		return _url;
	}

}
